<template>
    <div class="views-fuwuyuyue-add">
        <div>
            <el-card class="box-card">
                <template #header>
                    <div class="clearfix">
                        <span class="title"> 添加服务预约 </span>
                    </div>
                </template>

                <el-form :model="form" ref="formModel" :label-width="labelWidth" status-icon validate-on-rule-change>
                    <el-form-item v-if="isRead" label="项目编号" prop="xiangmubianhao"> {{ form.xiangmubianhao }} </el-form-item>

                    <el-form-item v-if="isRead" label="项目名称" prop="xiangmumingcheng"> {{ form.xiangmumingcheng }} </el-form-item>

                    <el-form-item v-if="isRead" label="项目分类" prop="xiangmufenlei">
                        <e-select-view module="fuwufenlei" :value="form.xiangmufenlei" select="id" show="fenleimingcheng"></e-select-view>
                    </el-form-item>

                    <el-form-item v-if="isRead" label="价格" prop="jiage" :rules="[{validator:rule.checkNumber, message:'输入一个有效数字'}]"> {{ form.jiage }} </el-form-item>

                    <el-form-item label="预约时间" prop="yuyueshijian" :rules="[{required:true, message:'请填写预约时间'}]">
                        <el-date-picker v-model="form.yuyueshijian" type="date" value-format="YYYY-MM-DD" placeholder="选择日期"> </el-date-picker>
                    </el-form-item>

                    <el-form-item label="预约时段" prop="yuyueshiduan">
                        <el-select v-model="form.yuyueshiduan"
                            ><el-option label="07:00-08:00" value="07:00-08:00"></el-option>
                            <el-option label="08:00-09:00" value="08:00-09:00"></el-option>
                            <el-option label="09:00-10:00" value="09:00-10:00"></el-option>
                            <el-option label="10:00-11:00" value="10:00-11:00"></el-option>
                            <el-option label="11:00-12:00" value="11:00-12:00"></el-option>
                            <el-option label="12:00-13:00" value="12:00-13:00"></el-option>
                            <el-option label="13:00-14:00" value="13:00-14:00"></el-option>
                            <el-option label="14:00-15:00" value="14:00-15:00"></el-option>
                            <el-option label="15:00-16:00" value="15:00-16:00"></el-option>
                            <el-option label="16:00-17:00" value="16:00-17:00"></el-option>
                            <el-option label="17:00-18:00" value="17:00-18:00"></el-option>
                            <el-option label="19:00-20:00" value="19:00-20:00"></el-option>
                            <el-option label="20:00-21:00" value="20:00-21:00"></el-option>
                        </el-select>
                    </el-form-item>

                    <el-form-item label="备注" prop="beizhu"> <el-input type="textarea" v-model="form.beizhu"></el-input> </el-form-item>

                    <el-form-item label="姓名" prop="xingming"> <el-input type="text" placeholder="输入姓名" style="width: 450px" v-model="form.xingming" /> </el-form-item>

                    <el-form-item label="联系方式" prop="lianxifangshi">
                        <el-input type="text" placeholder="输入联系方式" style="width: 450px" v-model="form.lianxifangshi" />
                    </el-form-item>

                    <el-form-item label="地址" prop="dizhi"> <el-input type="text" placeholder="输入地址" style="width: 450px" v-model="form.dizhi" /> </el-form-item>

                    <el-form-item label="定位" prop="dingwei">
                        <e-baidu-address v-model="form.dingwei" v-model:lng="form.jingdu" v-model:lat="form.jingdu" v-model:hash="form.hash"></e-baidu-address>
                    </el-form-item>

                    <el-form-item label="预约人" prop="yuyueren"> <el-input v-model="form.yuyueren" readonly style="width: 250px"></el-input> </el-form-item>

                    <el-form-item v-if="btnText">
                        <el-button type="primary" @click="submit">{{ btnText }}</el-button>
                    </el-form-item>
                </el-form></el-card
            >
        </div>
    </div>
</template>

<script setup>
    import http from "@/utils/ajax/http";
    import DB from "@/utils/db";
    import rule from "@/utils/rule";
    import router from "@/router";

    import { ref, reactive, computed } from "vue";
    import { useRoute } from "vue-router";
    import { session } from "@/utils/utils";
    import { ElMessage, ElMessageBox } from "element-plus";
    import { useFuwuyuyueCreateForm, canFuwuyuyueInsert } from "@/module";

    const route = useRoute();
    const props = defineProps({
        id: [String, Number],
        btnText: {
            type: String,
            default: "保存",
        },
        isRead: {
            type: Boolean,
            default: true,
        },
        isHouxu: {
            type: Boolean,
            default: true,
        },
        labelWidth: {
            type: String,
            default: "140px",
        },
    });
    const { form, readMap } = useFuwuyuyueCreateForm(props.id);
    const emit = defineEmits(["success"]);
    const formModel = ref();
    const loading = ref(false);
    var submit = () => {
        return new Promise((resolve, reject) => {
            formModel.value
                .validate()
                .then((res) => {
                    if (loading.value) return;
                    loading.value = true;
                    canFuwuyuyueInsert(form).then(
                        (res) => {
                            loading.value = false;
                            if (res.code == 0) {
                                emit("success", res.data);
                                if (props.isHouxu) {
                                    ElMessage.success("添加成功");
                                    router.go(-1);
                                }
                            } else {
                                ElMessageBox.alert(res.msg);
                            }
                            resolve(res);
                        },
                        (err) => {
                            loading.value = false;
                            ElMessageBox.alert(err.message);
                            reject(err);
                        }
                    );
                })
                .catch((err) => {
                    reject(err);
                });
        });
    };
</script>

<style scoped lang="scss">
    .views-fuwuyuyue-add {
    }
</style>
